package com.huazi.www.yang.medicine.servlet;

import com.alibaba.fastjson.JSON;
import com.huazi.www.yang.medicine.entity.DrugStock;
import com.huazi.www.yang.medicine.entity.Medicne;
import com.huazi.www.yang.medicine.service.Imlp.MedicneStockServicImpl;
import com.huazi.www.yang.medicine.service.MedicneStockService;
import com.huazi.www.yang.medicine.uilt.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 库房申请
 */

public class MedicneStockServlet extends HttpServlet {
    MedicneStockService medicne = new MedicneStockServicImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String tp = req.getParameter("tp");
        if (tp == null) {
            tp = "select";
        }
        if (tp.equals("select")) {
            String name = req.getParameter("name");//申请人
            String yaoName = req.getParameter("yaoName");//药名
            String curr = req.getParameter("currentPage");
            if (curr == null) {
                curr = "1";
            }
            Integer currentPage = Integer.parseInt(curr);//页码
            String applystatu = req.getParameter("applystatu");
            if (applystatu == null) {
                applystatu = "0";
            }
            Integer statu = Integer.parseInt(applystatu);
            Page<Medicne> page = medicne.selectStock(name, yaoName, statu, currentPage);
            req.setAttribute("page", page);
            req.setAttribute("id", 1);
            req.getRequestDispatcher("frontdesk/inMedicine/medicine.jsp").forward(req, resp);
        } else if (tp.equals("selectStatus")) {
            String name = req.getParameter("name");//申请人
            String yaoName = req.getParameter("yaoName");//药名
            String curr = req.getParameter("currentPage");
            if (curr == null) {
                curr = "1";
            }
            Integer currentPage = Integer.parseInt(curr);//页码
            String applystatu = req.getParameter("applystatu");
            System.out.println(applystatu + "zf");
            Integer statu = Integer.parseInt(applystatu);
            System.out.println(statu + "sz");
            Page<Medicne> page = medicne.selectStock(name, yaoName, statu, currentPage);
            req.setAttribute("id", 0);
            req.setAttribute("page", page);
            req.getRequestDispatcher("frontdesk/inMedicine/medicine.jsp").forward(req, resp);
        } else if (tp.equals("consent")) {
            //修改状态  applystatu
            String id = req.getParameter("id");
            Integer ids = Integer.parseInt(id);
            medicne.updateStatus(ids);
            PrintWriter out = resp.getWriter();
            out.print("true");
            out.flush();
            out.close();
        } else if (tp.equals("del")) {
            String id = req.getParameter("ids");
            Integer ids = Integer.parseInt(id);
            medicne.delete(ids);
            PrintWriter out = resp.getWriter();
            out.print("true");
            out.flush();
            out.close();
        } else if (tp.equals("details")) {
            String time = req.getParameter("time");
            System.out.println(time);
            List<DrugStock> list = medicne.selectDetails(time);
            PrintWriter out = resp.getWriter();
            String lis = JSON.toJSONString(list);
            out.print(lis);
            out.flush();
            out.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
